package com.yxjx.duoxue.comment;

import com.yxjx.duoxue.R;
import com.yxjx.duoxue.course.CourseIntroductionActivity;
import com.yxjx.duoxue.course.SchoolIntroductionActivity;
import com.yxjx.duoxue.datamodel.DMComment;
import com.yxjx.duoxue.datamodel.course.DMCourse;
import com.yxjx.duoxue.datamodel.school.DMSchool;
import com.yxjx.duoxue.network.NwUtils;
import com.yxjx.duoxue.utils.Utils;
import com.yxjx.duoxue.utils.UIHelper;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

public class MyCommentItemView extends LinearLayout {
	
	public static final int MODE_COMMENT_LIST = 0; // 点评详情中的控件
	public static final int MODE_COMMENT_FUNCTION = 1; // 课程／学校介绍中的控件

	private View mRoot;
	private TextView mViewReplies;
	private CommentRespondsListView mAllReplies;
	private ExpandableTextView mCommentText;
	private AllThumbnailsView mAllThumbnailsView;
	private TextView mTypeName;
	private TextView mRatingText;

	public MyCommentItemView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs);
		mRoot = LayoutInflater.from(context).inflate(R.layout.item_my_comment_record, this, true);
		mViewReplies = (TextView) mRoot.findViewById(R.id.view_responds);
		mAllReplies = (CommentRespondsListView) mRoot.findViewById(R.id.comment_respond_list);
		mCommentText = (ExpandableTextView) mRoot.findViewById(R.id.comment_text);
		mAllThumbnailsView = (AllThumbnailsView) mRoot.findViewById(R.id.all_images);
		mCommentDate = (TextView) mRoot.findViewById(R.id.comment_date);
		mRating = (RatingBar) mRoot.findViewById(R.id.rating_bar);
		mRatingText = (TextView) mRoot.findViewById(R.id.rating_value);
		mTypeName = (TextView) mRoot.findViewById(R.id.type_name);
		
		// click event handling.
		mViewReplies.setOnClickListener(mClickListener);
		mTypeName.setOnClickListener(mClickListener);
	}
	
	public MyCommentItemView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
	
	private View.OnClickListener mClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.view_responds:
				if (mAllReplies.getVisibility() != View.VISIBLE) {
					mAllReplies.setVisibility(View.VISIBLE);
				} else {
					mAllReplies.setVisibility(View.GONE);
				}
				break;
			case R.id.type_name:
				// 课程
				if (mComment.getCommentType() == 1) {
					Intent intent = new Intent();
					intent.putExtra(NwUtils.KEY_ID, "" + mComment.getTypeId());
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.setClass(getContext(), CourseIntroductionActivity.class);
					getContext().startActivity(intent);
				// 学校
				} else if (mComment.getCommentType() == 3) {
					Intent intent = new Intent();
					intent.putExtra(NwUtils.KEY_ID, "" + mComment.getTypeId());
					intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.setClass(getContext(), SchoolIntroductionActivity.class);
					getContext().startActivity(intent);
				}
				break;
			default:
				break;
			}
		}
	};
	private TextView mCommentDate;
	private RatingBar mRating;
	private DMComment mComment;
	
	// for listview re-use.
	public void reset() {
		
	}
	
	// for listview initialization
	public void update() {
		
	}
	
	// init 
	public void setComment(DMComment comment) {
		mComment = comment;
		mCommentDate.setText(Utils.getTimeStamp(comment.getCreateTime(), "yyyy-MM-dd HH:mm"));
		mRating.setRating(comment.getLevel().getTotalLevel());
		mRatingText.setText("" + comment.getLevel().getTotalLevel());
		mCommentText.setText(comment.getCommentText());
		mAllReplies.setData(comment.getReplys());
		mAllThumbnailsView.setData(comment.getImages());
		boolean shouldDismissFooter = (comment.getReplys() == null || comment.getReplys().size() == 0);
		mViewReplies.setVisibility(shouldDismissFooter ? View.GONE : View.VISIBLE);
		mTypeName.setText(comment.getTypeName());
	}
	
	/**
	 * 将课程，学校介绍页面的该控件设置为不可展开，不显示回复信息。
	 */
	public void setAsCommentFunctionMode() {
		mCommentText.setExpandable(false);
		mCommentText.setClickable(false);
		mViewReplies.setVisibility(View.GONE);
		mAllReplies.setVisibility(View.GONE);
	}
}
